﻿Imports _1065081_1065104.KHDTO
Imports _1065081_1065104.KHBUS
Imports _1065081_1065104.Global_Class

Public Class frmaddnhanvien
    Private _nvbus As New Cbusikhachhang()
#Region " form moving"
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Private Declare Function ReleaseCapture Lib "user32" () As Long
    Private Sub BorderlessWindowDrag(ByVal sender As Object, ByVal e As MouseEventArgs)
        ReleaseCapture()
        SendMessage(Me.Handle, &HA1, 2, 0&)
    End Sub
    Private Sub frmAddnhanvien_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        BorderlessWindowDrag(sender, e)
    End Sub
#End Region
  
#Region "Windows Events"
    Private Sub frmaddnhanvien_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        xoa.Enabled = False
        sua.Enabled = False
        luu.Enabled = False

    End Sub
#End Region
#Region "Windows Events"
    Private Sub them_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles them.Click
        ReNew()
        makh.Text = _nvbus.GetnhanvienID("AGBC", "MAKH", "KHACHHANG", 10).Trim()
        tenkh.Focus()

        luu.Enabled = True
        them.Enabled = False
        them.Enabled = False
    End Sub

    Private Sub luu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles luu.Click
        Try
            If CheckDataInput() = True Then
                _nvbus.Insert(AddkhachhangValue())
                sua.Enabled = True
                xoa.Enabled = True
                luu.Enabled = False
                them.Enabled = True
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Loi xu ly !", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try
    End Sub

    Private Sub boqua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles boqua.Click
        ReNew()
    End Sub

    Private Sub sua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles sua.Click
        Try
            If CheckDataInput() = True Then
                _nvbus.Update(AddkhachhangValue())
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Loi xu ly !", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try
    End Sub

    Private Sub xoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xoa.Click
        Try
            If MessageBox.Show("Bạn có thật sự muốn xóa ?", "Xac nhan", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) = DialogResult.Yes Then
                _nvbus.Delete(makh.Text.Trim())
                ReNew()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Loi xu ly !", MessageBoxButtons.OK, MessageBoxIcon.[Error])
        End Try
    End Sub

    Private Sub cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancel.Click
        Me.Close()
    End Sub
#End Region
#Region "Process"
    Private Function AddkhachhangValue() As Cinforkhachhang
        Dim khachhang As New Cinforkhachhang()
        khachhang.nhanvienid = makh.Text.Trim()
        khachhang.nhanvienname = tenkh.Text.Trim()
        khachhang.birthday = DateTimePicker1.Value
        khachhang.indentitycard = cmnd.Text.Trim()
        khachhang.phonenumber = dt.Text.Trim()
        'khachhang.Address = diachi.Text.Trim()
        khachhang.email = email.Text.Trim()

        Return khachhang
    End Function
    Private Function CheckDataInput() As Boolean
        Dim strErr As String = String.Empty

        If makh.Text.Trim() = "" Then
            strErr = "- Mã khách hàng."
        End If
        If tenkh.Text.Trim() = "" Then
            strErr = strErr & vbLf & "- Tên khách hàng."
        End If
        If cmnd.Text.Trim() = "" Then
            strErr = strErr & vbLf & "- Số chứng minh nhân dân."
        End If
        If diachi.Text.Trim() = "" Then
            strErr = strErr & vbLf & "- Địa chỉ khách hàng."
        End If
        If DateTime.Now.Year - DateTimePicker1.Value.Year < 15 Then
            strErr = strErr & vbLf & "- Ngày sinh không hợp lệ."
        End If

        If strErr <> String.Empty Then
            MessageBox.Show("Bạn phải hoàn tất các thông tin sau :" & vbLf & strErr, "Canh bao !", MessageBoxButtons.OK, MessageBoxIcon.[Error])
            Return False
        End If

        Return True
    End Function
    Private Sub ReNew()
        makh.Text = ""
        tenkh.Text = ""
        DateTimePicker1.Value = DateTime.Now
        cmnd.Text = ""
        dt.Text = ""
        diachi.Text = ""
        email.Text = ""

        them.Enabled = True
        luu.Enabled = False
        sua.Enabled = False
        xoa.Enabled = False
    End Sub
#End Region
End Class